package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.TeacherHistoryNewMapper;
import com.zhijian.medical.entity.po.TeacherHistoryNew;
import com.zhijian.medical.entity.vo.request.TeacherHistoryNewSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.TeacherHistoryNewSearchVo;
import com.zhijian.medical.entity.vo.response.TeacherHistoryNewVo;
import com.zhijian.medical.service.TeacherHistoryNewService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 带教记录新业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-12-24 14:49:30
 */
@Slf4j
@Service
public class TeacherHistoryNewServiceImpl implements TeacherHistoryNewService {

	@Autowired
    private TeacherHistoryNewMapper mapper;

    @Override
    public TeacherHistoryNew selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public TeacherHistoryNewVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(TeacherHistoryNew teacherHistoryNew) {
        Date now = new Date();
        teacherHistoryNew.setCreateTime(now);
        teacherHistoryNew.setUpdateTime(now);
        return mapper.insert(teacherHistoryNew) > 0 ? teacherHistoryNew.getId() : 0L;
    }

    @Override
    public Long insertSelective(TeacherHistoryNew teacherHistoryNew) {
        Date now = new Date();
        teacherHistoryNew.setCreateTime(now);
        teacherHistoryNew.setUpdateTime(now);
        return mapper.insertSelective(teacherHistoryNew) > 0 ? teacherHistoryNew.getId() : 0L;
    }

    @Override
    public boolean update(TeacherHistoryNew teacherHistoryNew) {
        Date now = new Date();
        teacherHistoryNew.setUpdateTime(now);
        return mapper.updateByPrimaryKey(teacherHistoryNew) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(TeacherHistoryNew record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(TeacherHistoryNewSaveOrUpdateVo saveOrUpdateVo) {
        TeacherHistoryNew teacherHistoryNew = new TeacherHistoryNew();
        BeanUtils.copyProperties(saveOrUpdateVo, teacherHistoryNew);
        return this.insert(teacherHistoryNew) != 0;
    }

    @Override
    public boolean modify(TeacherHistoryNewSaveOrUpdateVo saveOrUpdateVo) {
        TeacherHistoryNew old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        TeacherHistoryNew teacherHistoryNew = new TeacherHistoryNew();
        BeanUtils.copyProperties(saveOrUpdateVo, teacherHistoryNew);
        return this.update(teacherHistoryNew);
    }

    @Override
    public TeacherHistoryNewVo selectOne(TeacherHistoryNewSearchVo teacherHistoryNewSearchVo) {
        return mapper.selectOne(teacherHistoryNewSearchVo);
     }

    @Override
    public Integer count(TeacherHistoryNewSearchVo teacherHistoryNewSearchVo) {
        return mapper.count(teacherHistoryNewSearchVo);
    }

    @Override
    public List<TeacherHistoryNewVo> selectList(TeacherHistoryNewSearchVo teacherHistoryNewSearchVo) {
        return mapper.selectList(teacherHistoryNewSearchVo);
    }

    @Override
    public Page<TeacherHistoryNewVo> selectPage(TeacherHistoryNewSearchVo teacherHistoryNewSearchVo) {
        AssertUtil.check(Objects.isNull(teacherHistoryNewSearchVo.getPageNum()) || Objects.isNull(teacherHistoryNewSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(teacherHistoryNewSearchVo);
        //分页
        Page<TeacherHistoryNewVo> page = new Page<>(true, count, teacherHistoryNewSearchVo.getPageNum(), teacherHistoryNewSearchVo.getPageSize());
        teacherHistoryNewSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<TeacherHistoryNewVo> responseVos = this.selectList(teacherHistoryNewSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<TeacherHistoryNew> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(TeacherHistoryNewSearchVo teacherHistoryNewSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(teacherHistoryNewSearchVo);
    }
    
}